package com.miplus.ccrm.core.service.utils;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.mapper.CfgRuleTaskMapper;
import com.miplus.ccrm.core.pojo.CfgRuleTask;
import com.miplus.ccrm.core.pojo.CfgRuleTaskSteps;
import com.miplus.ccrm.core.pojo.LogXxlJob;
import com.miplus.ccrm.core.service.strategy.CfgRuleTaskStepsService;
import com.miplus.ccrm.core.utils.EnumRuleTaskRunStatus;
import com.miplus.ccrm.core.utils.EnumXxlLogOpType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;


/**
 * @Author： Yaohui
 * @Date： 2024/11/23 18:54
 * @Describe：
 */
@Service
@Component
@Slf4j
public class ExecutorServiceImpl {

    @Resource
    LogXxlJobService logXxlJobService;

    @Resource
    CfgRuleTaskStepsService cfgRuleTaskStepsService;

    @Resource
    CfgRuleTaskMapper cfgRuleTaskMapper;
    /**
     * 执行策略任务
     *
     */
    @Async("syncTaskExecutor")
    public void executeRuleTask(String usrId, Long ruleTaskId, LogXxlJob logXxlJob, List<CfgRuleTaskSteps> cfgRuleTaskStepsList)
    {

        try {
            for (CfgRuleTaskSteps cfgRuleTaskSteps : cfgRuleTaskStepsList) {
                logXxlJob.setRuleStepId(cfgRuleTaskSteps.getRuleStepId());
                logXxlJob.setExecStatus(0);
                logXxlJob.setActionType(cfgRuleTaskSteps.getActionCode());
                logXxlJob.setExecBeginTime(new Date());
                logXxlJobService.log(logXxlJob, EnumXxlLogOpType.RuleStep.value, 0, 0, 0, null, "策略开始执行...");
                CmdResVo cmdResVo = cfgRuleTaskStepsService.runRuleStep(usrId, cfgRuleTaskSteps);
                logXxlJobService.log(logXxlJob, EnumXxlLogOpType.RuleStep.value, 0, cmdResVo.getCount(), cmdResVo.getCount() < 0 ? 2 : 1, null, "策略执行结束。" + cmdResVo.getKeyId());
            }
            logXxlJob.setRuleStepId(null);
            logXxlJobService.log(logXxlJob, EnumXxlLogOpType.TaskFinish.value, 0, 0, 1, null, "任务执行完成。");
        }
        catch(Exception e)
        {
            logXxlJobService.log(logXxlJob, EnumXxlLogOpType.TaskFinish.value, 0, 0, 1, null, "任务异常结束。"+ e);
        }
        cfgRuleTaskMapper.update(null, new UpdateWrapper<CfgRuleTask>()
                .set("execute_status", EnumRuleTaskRunStatus.OK.value)
                .set("run_end_time",new Date())
                .eq("rule_task_id",ruleTaskId)
        );

    }








//    public void syncTaskExecutor(int i){
//        SimpleDateFormat formatterAll = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//
//        System.out.println("开始:::"+i+":"+formatterAll.format(new Date()));
//
//        try {
//
//            Random random=new Random();
//            int i1 = ( 1000+random.nextInt(10000000));
////            int i = random(0, 1000);
//            String result = String.format("1111111111这是第 %s 次,睡眠 %s ms", i, i1);
//            System.out.println(result);
//            TimeUnit.MILLISECONDS.sleep(i1);
//
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        System.out.println("结束:::"+i+":"+formatterAll.format(new Date()));
//
////        System.out.println("执行........");
//
//    }


}
